//
//  FindVC.m
//  K8彩票
//
//  Created by zyn on 2017/6/6.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "FindVC.h"
#import "FindLeftCell.h"
#import "FindLeftModel.h"
#import "KuaiSanCell.h"
#import "PaiLie5Cell.h"
#import "ShuangSeQiuCell.h"
#import "CaipiaoHistoryModel.h"
#import "FindDetailVC.h"
#import "ShuangSeVC.h"
#import "DaLeTouVC.h"
#import "PaiLieVC.h"
#import "PaiLie5VC.h"
#import "ResultDialog.h"

@interface FindVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *arrayLeft; // 中奖信息列表
    
    NSMutableArray *arrayShuangSeQiu;
    NSMutableArray *arrayDaLeTou;
    NSMutableArray *arrayPaiLie5;
    NSMutableArray *arrayPaiLie3;
    NSMutableArray *arrayJiangSuKuai3;
    NSMutableArray *arrayHuBeiKuai3;

}

@property (weak, nonatomic) IBOutlet UIButton *btnLeft; // 中奖信息
@property (weak, nonatomic) IBOutlet UIButton *btnRight; // 开奖号码
@property (weak, nonatomic) IBOutlet UITableView *tableLeft;
@property (weak, nonatomic) IBOutlet UITableView *tableRight;
@end

@implementation FindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeView];
    [self initializeData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    ((ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController).mainTabbar.frame = CGRectMake(0, ScreenHeight() - 49, ScreenWidth(), 49);
}

-(void)initializeView{
    self.navigationController.navigationBarHidden = YES;
    
    _btnLeft.layer.borderColor = [[UIColor whiteColor]CGColor];
    _btnLeft.layer.borderWidth = 1;
    _btnLeft.layer.cornerRadius = 4;
    _btnRight.layer.borderColor = [[UIColor whiteColor]CGColor];
    _btnRight.layer.borderWidth = 1;
    _btnRight.layer.cornerRadius = 4;
    
//    [_tableLeft setHidden:NO];
//    [_tableRight setHidden:YES];
    [_tableLeft setHidden:YES];
    [_tableRight setHidden:NO];
}
- (IBAction)onClickRight:(id)sender {
    int type = arc4random() % 6;
    int typeResult;
    if (type == 0) {
        typeResult = 11;
    }
    if (type == 1) {
        typeResult = 14;
    }
    if (type == 2) {
        typeResult = 16;
    }
    if (type == 3) {
        typeResult = 17;
    }
    if (type == 4) {
        typeResult = 75;
    }
    if (type == 5) {
        typeResult = 80;
    }

    ResultDialog *dialog = [[ResultDialog alloc]initWithNumber:123 type:typeResult vc:self];
    [self.tabBarController.view addSubview:dialog];
    [dialog show];
}

-(void)initializeData{
    arrayShuangSeQiu = [NSMutableArray arrayWithCapacity:0];
    arrayDaLeTou = [NSMutableArray arrayWithCapacity:0];
    arrayPaiLie5 = [NSMutableArray arrayWithCapacity:0];
    arrayPaiLie3 = [NSMutableArray arrayWithCapacity:0];
    arrayJiangSuKuai3 = [NSMutableArray arrayWithCapacity:0];
    arrayHuBeiKuai3 = [NSMutableArray arrayWithCapacity:0];

    
    _btnLeft.tag = 0;
    _btnRight.tag = 1;
    [_btnLeft addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [_btnRight addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 列表
    [self setArrayLeftDatas];
    
    _tableLeft.delegate = self;
    _tableLeft.dataSource = self;
    _tableLeft.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableRight.delegate = self;
    _tableRight.dataSource = self;
    _tableRight.separatorStyle = UITableViewCellSelectionStyleNone;
    
    
    [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"11",@"issueno":@"",@"start":@"0",@"num":@"3"} caipiaoid:11]; // 双色球
    [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"14",@"issueno":@"",@"start":@"0",@"num":@"3"} caipiaoid:14]; // 大乐透
    [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"75",@"issueno":@"",@"start":@"0",@"num":@"3"} caipiaoid:75]; // 江苏快三
    [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"80",@"issueno":@"",@"start":@"0",@"num":@"3"} caipiaoid:80]; // 湖北快三
    [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"17",@"issueno":@"",@"start":@"0",@"num":@"3"} caipiaoid:17]; // 排列五
    [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"16",@"issueno":@"",@"start":@"0",@"num":@"3"} caipiaoid:16]; // 排列三

}

-(void)onClick:(UIButton *)btn{
    switch (btn.tag) {
        case 0:
            // 中奖信息
            [_btnLeft setBackgroundColor:[UIColor whiteColor]];
            [_btnLeft setTitleColor:[dataManager rgba_16:@"FE3C3C"] forState:UIControlStateNormal];
            [_btnRight setBackgroundColor:[UIColor clearColor]];
            [_btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_tableLeft setHidden:NO];
            [_tableRight setHidden:YES];
            break;
            
        case 1:
            // 开奖号码
            [_btnRight setBackgroundColor:[UIColor whiteColor]];
            [_btnRight setTitleColor:[dataManager rgba_16:@"FE3C3C"] forState:UIControlStateNormal];
            [_btnLeft setBackgroundColor:[UIColor clearColor]];
            [_btnLeft setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_tableRight setHidden:NO];
            [_tableLeft setHidden:YES];
            break;
            
        default:
            break;
    }
}


#pragma mark - tableview
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableLeft) {
        return arrayLeft.count;
    }
    return 6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableLeft) {
        FindLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindLeftCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"FindLeftCell" owner:self options:nil]firstObject];
        }
        
        [cell setContent:arrayLeft[indexPath.row]];
        ///赋值
        return cell;
    }
    
    // ====右侧====
    if (indexPath.row == 0 || indexPath.row == 3) {
        KuaiSanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KuaiSanCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"KuaiSanCell" owner:self options:nil]firstObject];
        }
        // 江苏快三赋值
        if (indexPath.row == 0&& arrayJiangSuKuai3.count == 3) {
            [cell setContentJiangSuKuai3:arrayJiangSuKuai3];
        }
        // 湖北快三赋值
        if (indexPath.row == 3&& arrayHuBeiKuai3.count == 3) {
            [cell setContentHuBeiKuai3:arrayHuBeiKuai3];
        }
        
        return cell;
    }
    
    else if(indexPath.row == 1 || indexPath.row == 4){
        ShuangSeQiuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShuangSeQiuCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ShuangSeQiuCell" owner:self options:nil]firstObject];
        }
        
        // 双色球赋值
        if (indexPath.row == 1 && arrayShuangSeQiu.count == 3) {
            [cell setContentShuangSeQiu:arrayShuangSeQiu];
        }
        // 大乐透赋值
        if (indexPath.row == 4 && arrayDaLeTou.count == 3) {
            [cell setContentDaLeTou:arrayDaLeTou];
        }
        return cell;
    }
    
    else{
        PaiLie5Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"PaiLie5Cell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PaiLie5Cell" owner:self options:nil]firstObject];
        }
        // 排列五赋值
        if (indexPath.row == 2&& arrayPaiLie5.count == 3) {
            [cell setContentPaiLie5:arrayPaiLie5];
        }
        if (indexPath.row == 5&& arrayPaiLie3.count == 3) {
            [cell setContentPaiLie3:arrayPaiLie3];
        }
        return cell;
    }
    
    
    
   
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == _tableLeft){
        return 60;
    }
    
    return 200;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == _tableLeft){
//        [self push:[PaiLie5VC class]];
        return;
    }
    [self push:[FindDetailVC class] bundle:^(id vc) {
        /// 双色球11 大乐透14 排列三16 排列五17 江苏快三75 湖北快三80
        switch (indexPath.row) {
            case 0:
                ((FindDetailVC *)vc).caipiaoType = @"75";
                break;
                
            case 1:
                ((FindDetailVC *)vc).caipiaoType = @"11";
                break;
                
            case 2:
                ((FindDetailVC *)vc).caipiaoType = @"17";
                break;
                
            case 3:
                ((FindDetailVC *)vc).caipiaoType = @"80";
                break;
                
            case 4:
                ((FindDetailVC *)vc).caipiaoType = @"14";
                break;
                
            case 5:
                ((FindDetailVC *)vc).caipiaoType = @"16";
                break;
                
            default:
                break;
        }
    }];

}


/// 中奖消息假数据
-(void)setArrayLeftDatas{
    FindLeftModel *model1 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"zyn******" category:@"江苏快三" money:@"1214"];
    FindLeftModel *model2 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"pt******" category:@"双色球" money:@"6577"];
    FindLeftModel *model3 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"aayd******" category:@"大乐透" money:@"2343"];
    FindLeftModel *model4 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"jkh******" category:@"江苏快三" money:@"787"];
    FindLeftModel *model5 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"kweh******" category:@"湖北快三" money:@"9832"];
    FindLeftModel *model6 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"fww******" category:@"双色球" money:@"8233"];
    FindLeftModel *model7 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"ghg******" category:@"重庆时时彩" money:@"2311"];
    FindLeftModel *model8 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"owh******" category:@"11选5" money:@"672"];
    FindLeftModel *model9 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"je******" category:@"双色球" money:@"5423"];
    FindLeftModel *model10 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"iejh******" category:@"江苏快三" money:@"463"];
    FindLeftModel *model11 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"gt******" category:@"湖北快三" money:@"9073"];
    FindLeftModel *model12 = [[FindLeftModel alloc]initWithPhoto:@"" name:@"ouw******" category:@"天津时时彩" money:@"865"];
    
    arrayLeft = [NSMutableArray arrayWithCapacity:0];
    [arrayLeft addObject:model1];
    [arrayLeft addObject:model2];
    [arrayLeft addObject:model3];
    [arrayLeft addObject:model4];
    [arrayLeft addObject:model5];
    [arrayLeft addObject:model6];
    [arrayLeft addObject:model7];
    [arrayLeft addObject:model8];
    [arrayLeft addObject:model9];
    [arrayLeft addObject:model10];
    [arrayLeft addObject:model11];
    [arrayLeft addObject:model12];
}

/// 请求开奖信息列表
/// 双色球11 大乐透14 排列三16 排列五17 江苏快三75 湖北快三80
- (void)requestCaipiaoList:(NSDictionary *)parameters caipiaoid:(NSInteger )caipiaoid{
    if (caipiaoid == 16)
        [self showHudInView:self.view hint:@""];
    
    [[HttpManager getInstance]get:URL_HSITORY params:parameters successCallback:^(NSString *state, id responseObject) {
        
        NSDictionary *result = [responseObject objectForKey:@"result"];
        NSArray *list = [result objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            CaipiaoHistoryModel *model = [[CaipiaoHistoryModel alloc]initWithDictionary:dic];
            switch (caipiaoid) {
                case 11:
                    [arrayShuangSeQiu addObject:model];
                    break;
                
                case 14:
                    [arrayDaLeTou addObject:model];
                    break;
                    
                case 75:
                    [arrayJiangSuKuai3 addObject:model];
                    break;
                    
                case 80:
                    [arrayHuBeiKuai3 addObject:model];
                    break;
                    
                case 17:
                    [arrayPaiLie5 addObject:model];
                    break;
                    
                case 16:
                    [arrayPaiLie3 addObject:model];
                    break;
                    
                default:
                    break;
            }
        }
        
        [_tableRight reloadData];
        [self hideHud];
        
    } failCallback:^(NSString *state, id responseObject) {
        [self hideHud];
    }];
}


@end
